home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual Basic 5 / Mastering Microsoft Visual Basic 5.ISO / demo code / ch11 / winsock1 / frmchat1.frm (.txt) next >
Encoding:
Visual Basic Form  |  1997-02-06  |  13.2 KB  |  437 lines

  1. VERSION 5.00
  2. Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
  3. Begin VB.Form frmChat1 
  4.    Caption         =   "Winsock Chatter"
  5.    ClientHeight    =   6075
  6.    ClientLeft      =   1710
  7.    ClientTop       =   1485
  8.    ClientWidth     =   6870
  9.    LinkTopic       =   "Form1"
  10.    LockControls    =   -1  'True
  11.    ScaleHeight     =   6075
  12.    ScaleWidth      =   6870
  13.    Begin VB.Timer Timer1 
  14.       Interval        =   1000
  15.       Left            =   1920
  16.       Top             =   7140
  17.    End
  18.    Begin MSWinsockLib.Winsock socServer 
  19.       Left            =   300
  20.       Top             =   7140
  21.       _ExtentX        =   741
  22.       _ExtentY        =   741
  23.    End
  24.    Begin MSWinsockLib.Winsock socClient 
  25.       Left            =   1080
  26.       Top             =   7140
  27.       _ExtentX        =   741
  28.       _ExtentY        =   741
  29.    End
  30.    Begin VB.Frame ssfReceive 
  31.       Caption         =   "&Server Info:"
  32.       Height          =   2955
  33.       Left            =   60
  34.       TabIndex        =   0
  35.       Top             =   60
  36.       Width           =   6735
  37.       Begin VB.TextBox txtRemotePort 
  38.          Height          =   315
  39.          Left            =   4500
  40.          TabIndex        =   6
  41.          Top             =   1200
  42.          Visible         =   0   'False
  43.          Width           =   675
  44.       End
  45.       Begin VB.ComboBox cboServerProtocol 
  46.          Height          =   315
  47.          ItemData        =   "frmChat1.frx":0000
  48.          Left            =   4500
  49.          List            =   "frmChat1.frx":0002
  50.          TabIndex        =   4
  51.          Top             =   780
  52.          Width           =   2055
  53.       End
  54.       Begin VB.TextBox txtReceivedData 
  55.          Height          =   675
  56.          Left            =   1140
  57.          MultiLine       =   -1  'True
  58.          TabIndex        =   16
  59.          TabStop         =   0   'False
  60.          Top             =   1680
  61.          Width           =   5415
  62.       End
  63.       Begin VB.CommandButton cmdCloseServerConnection 
  64.          Caption         =   "&Close"
  65.          Height          =   315
  66.          Left            =   2280
  67.          TabIndex        =   8
  68.          Top             =   2460
  69.          Width           =   915
  70.       End
  71.       Begin VB.CommandButton cmdListen 
  72.          Caption         =   "&Listen"
  73.          Height          =   315
  74.          Left            =   1140
  75.          TabIndex        =   7
  76.          Top             =   2460
  77.          Width           =   915
  78.       End
  79.       Begin VB.TextBox txtServerState 
  80.          Enabled         =   0   'False
  81.          Height          =   315
  82.          Left            =   1140
  83.          TabIndex        =   14
  84.          TabStop         =   0   'False
  85.          Top             =   1200
  86.          Width           =   1995
  87.       End
  88.       Begin VB.TextBox txtLocalIPAddress 
  89.          Enabled         =   0   'False
  90.          Height          =   315
  91.          Left            =   1140
  92.          TabIndex        =   12
  93.          TabStop         =   0   'False
  94.          Top             =   780
  95.          Width           =   1995
  96.       End
  97.       Begin VB.TextBox txtLocalPort 
  98.          Height          =   315
  99.          Left            =   4500
  100.          TabIndex        =   2
  101.          Top             =   360
  102.          Width           =   675
  103.       End
  104.       Begin VB.TextBox txtLocalHost 
  105.          Enabled         =   0   'False
  106.          Height          =   315
  107.          Left            =   1140
  108.          TabIndex        =   10
  109.          TabStop         =   0   'False
  110.          Top             =   360
  111.          Width           =   1995
  112.       End
  113.       Begin VB.Label lblRemotePort 
  114.          Caption         =   "Remote Port:"
  115.          Height          =   195
  116.          Left            =   3360
  117.          TabIndex        =   5
  118.          Top             =   1260
  119.          Visible         =   0   'False
  120.          Width           =   1095
  121.       End
  122.       Begin VB.Label lblServerProtocol 
  123.          Caption         =   "&Protocol:"
  124.          Height          =   255
  125.          Left            =   3360
  126.          TabIndex        =   3
  127.          Top             =   840
  128.          Width           =   915
  129.       End
  130.       Begin VB.Label lblReceivedData 
  131.          Caption         =   "Rec'd Data:"
  132.          Height          =   255
  133.          Left            =   180
  134.          TabIndex        =   15
  135.          Top             =   1740
  136.          Width           =   915
  137.       End
  138.       Begin VB.Label lblServerState 
  139.          Caption         =   "State:"
  140.          Height          =   255
  141.          Left            =   180
  142.          TabIndex        =   13
  143.          Top             =   1200
  144.          Width           =   915
  145.       End
  146.       Begin VB.Label lblLocalIPAddress 
  147.          Caption         =   "Local IP:"
  148.          Height          =   255
  149.          Left            =   180
  150.          TabIndex        =   11
  151.          Top             =   780
  152.          Width           =   915
  153.       End
  154.       Begin VB.Label lblLocalPort 
  155.          Caption         =   "Local Port:"
  156.          Height          =   195
  157.          Left            =   3360
  158.          TabIndex        =   1
  159.          Top             =   420
  160.          Width           =   855
  161.       End
  162.       Begin VB.Label lblLocalHost 
  163.          Caption         =   "&Local Host:"
  164.          Height          =   255
  165.          Left            =   180
  166.          TabIndex        =   9
  167.          Top             =   420
  168.          Width           =   915
  169.       End
  170.    End
  171.    Begin VB.Frame ssfRemote 
  172.       Caption         =   "Client Info:"
  173.       Height          =   2895
  174.       Left            =   60
  175.       TabIndex        =   17
  176.       Top             =   3120
  177.       Width           =   6735
  178.       Begin VB.TextBox txtClientLocalPort 
  179.          Height          =   315
  180.          Left            =   4560
  181.          TabIndex        =   25
  182.          Top             =   1200
  183.          Visible         =   0   'False
  184.          Width           =   675
  185.       End
  186.       Begin VB.ComboBox cboClientProtocol 
  187.          Height          =   315
  188.          ItemData        =   "frmChat1.frx":0004
  189.          Left            =   4560
  190.          List            =   "frmChat1.frx":0006
  191.          TabIndex        =   23
  192.          Top             =   780
  193.          Width           =   2055
  194.       End
  195.       Begin VB.TextBox txtHostIPAddress 
  196.          Enabled         =   0   'False
  197.          Height          =   315
  198.          Left            =   1080
  199.          TabIndex        =   32
  200.          TabStop         =   0   'False
  201.          Top             =   780
  202.          Width           =   1995
  203.       End
  204.       Begin VB.CommandButton cmdCloseClientConnection 
  205.          Caption         =   "C&lose"
  206.          Height          =   315
  207.          Left            =   3660
  208.          TabIndex        =   30
  209.          Top             =   2460
  210.          Width           =   1095
  211.       End
  212.       Begin VB.CommandButton cmdSend 
  213.          Caption         =   "&Send"
  214.          Height          =   315
  215.          Left            =   2340
  216.          TabIndex        =   29
  217.          Top             =   2460
  218.          Width           =   1095
  219.       End
  220.       Begin VB.CommandButton cmdConnect 
  221.          Caption         =   "&Connect"
  222.          Height          =   315
  223.          Left            =   1080
  224.          TabIndex        =   26
  225.          Top             =   2460
  226.          Width           =   1095
  227.       End
  228.       Begin VB.TextBox txtClientData 
  229.          Height          =   675
  230.          Left            =   1080
  231.          MultiLine       =   -1  'True
  232.          TabIndex        =   28
  233.          Top             =   1680
  234.          Width           =   5475
  235.       End
  236.       Begin VB.TextBox txtClientState 
  237.          Enabled         =   0   'False
  238.          Height          =   315
  239.          Left            =   1080
  240.          TabIndex        =   34
  241.          TabStop         =   0   'False
  242.          Top             =   1200
  243.          Width           =   1995
  244.       End
  245.       Begin VB.TextBox txtHostPort 
  246.          Height          =   315
  247.          Left            =   4560
  248.          TabIndex        =   21
  249.          Top             =   360
  250.          Width           =   675
  251.       End
  252.       Begin VB.TextBox txtHost 
  253.          Height          =   315
  254.          Left            =   1080
  255.          TabIndex        =   19
  256.          Top             =   360
  257.          Width           =   1995
  258.       End
  259.       Begin VB.Label lblClientLocalPort 
  260.          Caption         =   "Local Port:"
  261.          Height          =   195
  262.          Left            =   3360
  263.          TabIndex        =   24
  264.          Top             =   1260
  265.          Visible         =   0   'False
  266.          Width           =   975
  267.       End
  268.       Begin VB.Label lblClientProtocol 
  269.          Caption         =   "&Protocol:"
  270.          Height          =   255
  271.          Left            =   3360
  272.          TabIndex        =   22
  273.          Top             =   840
  274.          Width           =   915
  275.       End
  276.       Begin VB.Label lblRemoteIP 
  277.          Caption         =   "&Host IP:"
  278.          Height          =   255
  279.          Left            =   120
  280.          TabIndex        =   31
  281.          Top             =   840
  282.          Width           =   915
  283.       End
  284.       Begin VB.Label lblClientData 
  285.          Caption         =   "Send Data:"
  286.          Height          =   255
  287.          Left            =   120
  288.          TabIndex        =   27
  289.          Top             =   1740
  290.          Width           =   915
  291.       End
  292.       Begin VB.Label lblClientState 
  293.          Caption         =   "State:"
  294.          Height          =   255
  295.          Left            =   120
  296.          TabIndex        =   33
  297.          Top             =   1200
  298.          Width           =   915
  299.       End
  300.       Begin VB.Label lblHostPort 
  301.          Caption         =   "Host Port:"
  302.          Height          =   195
  303.          Left            =   3360
  304.          TabIndex        =   20
  305.          Top             =   420
  306.          Width           =   975
  307.       End
  308.       Begin VB.Label lblHost 
  309.          Caption         =   "&Host"
  310.          Height          =   255
  311.          Left            =   120
  312.          TabIndex        =   18
  313.          Top             =   420
  314.          Width           =   915
  315.       End
  316.    End
  317. Attribute VB_Name = "frmChat1"
  318. Attribute VB_GlobalNameSpace = False
  319. Attribute VB_Creatable = False
  320. Attribute VB_PredeclaredId = True
  321. Attribute VB_Exposed = False
  322. Option Explicit
  323. Private Sub Form_Load()
  324.   On Error GoTo Err_Handler
  325.   InitializeConnectionInfo
  326.   InitializeServerInfo
  327.   SetProtocolCombobox cboClientProtocol
  328.   Exit Sub
  329. Err_Handler:
  330.   MsgBox Err.Description
  331. End Sub
  332. Private Sub cboClientProtocol_Click()
  333.   Dim bTCP As Boolean
  334.   bTCP = (cboClientProtocol.ListIndex = 0)
  335.   lblClientLocalPort.Visible = Not bTCP
  336.   txtClientLocalPort.Visible = Not bTCP
  337. End Sub
  338. Private Sub cboServerProtocol_Click()
  339.   Dim bTCP As Boolean
  340.   bTCP = (cboServerProtocol.ListIndex = 0)
  341.   lblRemotePort.Visible = Not bTCP
  342.   txtRemotePort.Visible = Not bTCP
  343. End Sub
  344. Private Sub cmdListen_Click()
  345.   On Error GoTo Err_Handler
  346.   With socServer
  347.     .Protocol = _
  348.       cboServerProtocol.ItemData(cboServerProtocol.ListIndex)
  349.     If .Protocol = sckTCPProtocol Then
  350.       .LocalPort = txtLocalPort
  351.       .Listen
  352.     Else
  353.       .RemotePort = txtRemotePort
  354.       .Bind txtLocalPort
  355.     End If
  356.   End With
  357.   Exit Sub
  358. Err_Handler:
  359.   MsgBox Err.Description
  360. End Sub
  361. Private Sub cmdConnect_Click()
  362.   On Error GoTo Err_Handler
  363.   With socClient
  364.     .RemoteHost = txtHost
  365.     .RemotePort = txtHostPort
  366.     .Protocol = _
  367.       cboClientProtocol.ItemData(cboClientProtocol.ListIndex)
  368.     If .Protocol = sckTCPProtocol Then
  369.       .Connect
  370.       txtHostIPAddress = socClient.RemoteHostIP
  371.     Else
  372.       .Bind txtClientLocalPort
  373.     End If
  374.   End With
  375.   Exit Sub
  376. Err_Handler:
  377.   MsgBox Err.Description
  378. End Sub
  379. Private Sub socServer_ConnectionRequest(ByVal requestID As Long)
  380.   On Error GoTo Err_Handler
  381.   With socServer
  382.     If .State <> sckClosed Then
  383.       .Close
  384.     End If
  385.     .Accept requestID
  386.   End With
  387.   Debug.Print "Connection Accepted with RequestID = " & Str$(requestID)
  388.   Exit Sub
  389. Err_Handler:
  390.   MsgBox Err.Description
  391. End Sub
  392. Private Sub cmdSend_Click()
  393.   On Error GoTo Err_Handler
  394.   socClient.SendData txtClientData
  395.   Exit Sub
  396. Err_Handler:
  397.   MsgBox Err.Description
  398. End Sub
  399. Private Sub socServer_DataArrival(ByVal bytesTotal As Long)
  400.   Dim sData As String
  401.   Debug.Print Str$(bytesTotal) & " of data arrived."
  402.   socServer.GetData sData
  403.   txtReceivedData = sData
  404. End Sub
  405. Private Sub cmdCloseClientConnection_Click()
  406.   On Error GoTo Err_Handler
  407.   socClient.Close
  408.   Exit Sub
  409. Err_Handler:
  410.   MsgBox Err.Description
  411. End Sub
  412. Private Sub cmdCloseServerConnection_Click()
  413.   On Error GoTo Err_Handler
  414.   socServer.Close
  415.   Exit Sub
  416. Err_Handler:
  417.   MsgBox Err.Description
  418. End Sub
  419. Private Sub Timer1_Timer()
  420.   ' Update Local Connection
  421.   GetConnectionState
  422. End Sub
  423. Private Sub InitializeServerInfo()
  424.   On Error GoTo Err_Handler
  425.   txtLocalHost = socServer.LocalHostName
  426.   txtLocalIPAddress = socServer.LocalIP
  427.   SetProtocolCombobox cboServerProtocol
  428.   GetConnectionState
  429.   Exit Sub
  430. Err_Handler:
  431.   MsgBox Err.Description
  432. End Sub
  433. Private Sub GetConnectionState()
  434.   txtServerState = strState(socServer.State)
  435.   txtClientState = strState(socClient.State)
  436. End Sub
  437.